//app.js
var Util = require('./utils/util.js')
App({
  onLaunch: function () {
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId

      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null
  },
  url: 'https://www.jishulang.cc/web1/api.php?task=',
  // url: 'http://192.168.3.11',
  // url:"https://v.isuoge.com",
  // ajax封装
  ajax(model) {
    var silent = model.silent || false;
    // 是否静默加载  默认false 显示加载loading
    if (!silent) {
      wx.showLoading({
        title: '加载中',
      })
    }

    //拼接url  
    model.url = this.url + model.url;

    //get参数拼接  
    if (model.method == "get" && model.data !== undefined) {
      for (let k in model.data) {
        if (model.data[k].toString() !== '') {
          model.url = model.url + "&" + k + "=" + model.data[k];
        }
      }
      console.log('get请求');
      model.data = '';
    }

    console.log('请求前的参数' + JSON.stringify(model))
    //返回Promise对象  
    return new Promise(
      function (resolve) {
        var datas = model.data;
        if (!datas){
          datas = {}
        }
        wx.request({
          url: model.url,
          data: Util.mysign(datas),
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 解决post接受不到参数的问题
          },
          success: (res) => {
            wx.hideLoading()
            if (res.statusCode == 200) {
              console.log('请求url====' + model.url)
              console.log('请求参数===' + JSON.stringify(model.data))
              resolve(res.data);
            } else {
              //错误信息处理  
              if (!silent) {
                wx.showModal({
                  title: '提示',
                  content: '服务器错误，请联系客服',
                  showCancel: false,
                })
              }

            }
          }
        })
      }
    )
  },
  // 去除html标签
  htmltotxt: function convertHtmlToText(inputText) {
    var returnText = "" + inputText;


    returnText = returnText.replace(/<\/div>/ig, '\r\n');
    returnText = returnText.replace(/<\/li>/ig, '\r\n');
    returnText = returnText.replace(/<li>/ig, '  *  ');
    returnText = returnText.replace(/<\/ul>/ig, '\r\n');
    //-- remove BR tags and replace them with line break
    returnText = returnText.replace(/<br\s*[\/]?>/gi, "\r\n");

    // 去除概况经理数据中空格符
    // returnText = returnText.replace(/&nbsp;/gi, "\r");

    //-- remove P and A tags but preserve what's inside of them
    returnText = returnText.replace(/<p.*?>/gi, "\r\n");
    returnText = returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");

    //-- remove all inside SCRIPT and STYLE tags
    returnText = returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
    returnText = returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
    //-- remove all else
    returnText = returnText.replace(/<(?:.|\s)*?>/g, "");

    //-- get rid of more than 2 multiple line breaks:
    returnText = returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\r\n\r\n");

    //-- get rid of more than 2 spaces:
    returnText = returnText.replace(/ +(?= )/g, '');

    //-- get rid of html-encoded characters:
    returnText = returnText.replace(/ /gi, " ");
    returnText = returnText.replace(/&/gi, "&");
    returnText = returnText.replace(/"/gi, '"');
    returnText = returnText.replace(/</gi, '<');
    returnText = returnText.replace(/>/gi, '>');

    return returnText;
  }

})